package com.lmt.api;

import com.lmt.base.ResultData;
import com.lmt.entity.UserInfo;
import com.lmt.entity.search.UserInfoSearch;
import com.lmt.service.UserInfoService;
import com.lmt.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author MT-Lin
 * @date 2018/1/3 8:55
 */
@RestController
@RequestMapping("/UserInfoApi")
public class UserInfoApi {

	private final UserInfoService userInfoService;

	@Autowired
	public UserInfoApi(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	@RequestMapping("/getUserInfo")
	public ResultData getUserInfo(@RequestBody UserInfoSearch userInfoSearch) {
		return this.userInfoService.getUserInfo(userInfoSearch.getTokenId());
	}

	@RequestMapping("/register")
	public ResultData register(@RequestBody UserInfo userInfo) throws Exception {
		// 用户注册
		userInfo.setUserType(SystemConstant.USER_TYPE_USER_CODE);
		return this.userInfoService.save(userInfo);
	}

	@RequestMapping("/login")
	public ResultData login(@RequestBody UserInfo userInfo, HttpSession session) throws Exception {
		return this.userInfoService.login(userInfo, session);
	}

	@RequestMapping("/search")
	public ResultData search(@RequestBody UserInfoSearch userInfoSearch) {
		return this.userInfoService.search(userInfoSearch);
	}

	@RequestMapping("/addAdmin")
	public ResultData addAdmin(@RequestBody UserInfo userInfo) throws Exception {
		// 管理员添加管理员
		userInfo.setUserType(SystemConstant.USER_TYPE_ADMIN_CODE);
		return this.userInfoService.save(userInfo);
	}

	@RequestMapping("/delete")
	public ResultData delete(@RequestBody List<UserInfo> userInfoList) {
		return this.userInfoService.delete(userInfoList);
	}

	@RequestMapping("/freeze")
	public ResultData freeze(@RequestBody List<UserInfo> userInfoList) {
		return this.userInfoService.freeze(userInfoList);
	}

	@RequestMapping("/unfreeze")
	public ResultData unfreeze(@RequestBody List<UserInfo> userInfoList) {
		return this.userInfoService.unfreeze(userInfoList);
	}

	@RequestMapping("/resetPassword")
	public ResultData resetPassword(@RequestBody List<UserInfo> userInfoList) throws Exception {
		return this.userInfoService.resetPassword(userInfoList);
	}
}
